package com.cloudkinto.controller.order.outbound;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.wave.*;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.entity.OutboundWaveDo;
import com.cloudkinto.service.order.outbound.OutboundWaveService;
import com.cloudkinto.vo.order.outbound.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Objects;

/**
 * <p>
 * 出库波次 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-12-03
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "wms波次管理")
@RequestMapping("/api/admin/wms/wave")
public class OutboundWaveWmsController extends BaseController {
    private final OutboundWaveService outboundWaveService;

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "出库波次根据id查看详情", notes = "出库波次根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功", response = WaveDetail.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", outboundWaveService.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "出库波次查询初始化", notes = "出库波次查询初始化")
    public SingleResult pageInit() {
        return outboundWaveService.pageInit(getCurrentUserId(), getTenantId(), "wms");
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "出库波次查询（分页）", notes = "出库波次查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功", response = OutboundWaveDo.class))
    public PageResult listPage(@RequestBody @Validated WaveQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setWhId(getWarehouseId());
        return outboundWaveService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/listPageStatusStatistics")
    @ApiOperation(value = "出库波次查询（分页）", notes = "出库波次查询（分页）")
    public SingleResult listPageStatusStatistics(@RequestBody @Validated WaveQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setWhId(getWarehouseId());
        bo.setStatus(null);
        return outboundWaveService.listPageStatusStatistics(bo);
    }

    @UserSessionRequired
    @PutMapping("/cannelWave/{id}")
    @ApiOperation(value = "取消波次", notes = "取消波次")
    public SingleResult cannelWave(@PathVariable("id") Long id) {
        return SingleResult.success("取消成功", outboundWaveService.cannelWave(id, getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PutMapping("/assignPickers")
    @ApiOperation(value = "指定拣货员", notes = "指定拣货员")
    public SingleResult assignPickers(@RequestParam("ids") Long[] ids, @RequestParam("pickerId") Long pickerId) {
        outboundWaveService.assignPickers(Arrays.asList(ids), pickerId);
        return SingleResult.success("指定成功");
    }

    @UserSessionRequired
    @GetMapping("/wavePickingDetail/{id}")
    @ApiOperation(value = "波次拣货查看详情", notes = "波次拣货查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = WavePickingDetail.class))
    public SingleResult wavePickingDetail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", outboundWaveService.wavePickingDetail(id));
    }


    @UserSessionRequired
    @PostMapping("wavePickingOperate")
    @ApiOperation(value = "波次拣货操作", notes = "波次拣货操作")
    @ApiResponse(code = 200, message = "操作成功 true全部拣货 false部分拣货")
    public SingleResult wavePickingOperate(@RequestBody @Validated WavePickingBo bo) {
        return SingleResult.success("操作成功",
                outboundWaveService.wavePickingOperate(bo, getCurrentUserId(), getUserName(), getTenantId()));
    }

    @UserSessionRequired
    @PutMapping("/waveForceFinish/{id}")
    @ApiOperation(value = "波次强制完结")
    public void printWavePickingListByTotal(@PathVariable("id") Long id) {
        outboundWaveService.waveForceFinish(id, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @GetMapping("/getSecondarySortingPreJudge/{waveNo}")
    @ApiOperation(value = "获取二次分拣前置判断", notes = "获取二次分拣前置判断")
    public SingleResult getSecondarySortingPreJudge(@PathVariable("waveNo") String waveNo) {
        outboundWaveService.getSecondarySortingPreJudge(waveNo);
        return SingleResult.success("查询成功");
    }

    @UserSessionRequired
    @PostMapping("/getSecondarySortingDetail")
    @ApiOperation(value = "获取二次分拣详情", notes = "获取二次分拣详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = WaveSecondarySortingDetail.class))
    public SingleResult getSecondarySortingDetail(@RequestBody @Validated SecondarySortingQueryBo bo) {
        return SingleResult.success("查询成功", outboundWaveService.getSecondarySortingDetail(bo, getWarehouseId()));
    }

    @UserSessionRequired
    @PostMapping("/secondarySortingOperate")
    @ApiOperation(value = "二次分拣操作", notes = "二次分拣操作")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = SecondarySortingOperateVo.class))
    public SingleResult secondarySortingOperate(@RequestBody @Validated WaveSecondarySortingBo bo) {
        return SingleResult.success("操作成功", outboundWaveService.secondarySortingOperate(bo, getCurrentUserId(), getUserName(), getWarehouseId()));
    }

    @UserSessionRequired
    @PostMapping("/secondarySortingClearRedo")
    @ApiOperation(value = "二次分拣清空重做操作", notes = "二次分拣清空重做操作")
    public SingleResult secondarySortingClearRedo(@RequestBody @Validated WaveSecondarySortingClearBo bo) {
        outboundWaveService.secondarySortingClearRedo(bo, getCurrentUserId(), getUserName());
        return SingleResult.success("操作成功");
    }

    @UserSessionRequired
    @PostMapping("/secondarySortingForceFinish")
    @ApiOperation(value = "二次分拣强制完结操作", notes = "二次分拣强制完结操作")
    public SingleResult secondarySortingForceFinish(@RequestBody @Validated WaveSecondarySortingForceFinishBo bo) {
        outboundWaveService.secondarySortingForceFinish(bo, getCurrentUserId(), getUserName());
        return SingleResult.success("操作成功");
    }

    @UserSessionRequired
    @GetMapping("/getRecheckDetail")
    @ApiOperation(value = "获取复核详情", notes = "获取复核详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = WaveRecheckDetail.class))
    public SingleResult getRecheckDetail(@RequestParam("scan") String scan, @RequestParam("whetherCheck") Boolean whetherCheck) {
        if (Objects.isNull(whetherCheck)) {
            whetherCheck = true;
        }
        return SingleResult.success("查询成功", outboundWaveService.getRecheckDetail(scan, whetherCheck, getWarehouseId()));
    }

    @UserSessionRequired
    @PostMapping("/recheck")
    @ApiOperation(value = "复核操作", notes = "复核操作")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = RecheckOperateVo.class))
    public SingleResult recheck(@RequestBody @Validated RecheckBo bo) {
        return SingleResult.success("操作成功", outboundWaveService.recheck(bo, getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PostMapping("/recheckClearRedo")
    @ApiOperation(value = "复核清空重做操作", notes = "复核清空重做操作")
    public SingleResult recheckClearRedo(@RequestBody @Validated RecheckClearRedoBo bo) {
        outboundWaveService.recheckClearRedo(bo, getCurrentUserId(), getUserName());
        return SingleResult.success("操作成功");
    }

    @UserSessionRequired
    @GetMapping("/printWavePickingListByClassify/{id}")
    @ApiOperation(value = "打印波次分拣拣货单", notes = "打印波次分拣拣货单")
    public void printWavePickingListByClassify(@PathVariable("id") Long id, HttpServletResponse response) throws Exception {
        outboundWaveService.printWavePickingListByClassify(id, response);
    }

    @UserSessionRequired
    @GetMapping("/printWavePickingListByTotal/{id}")
    @ApiOperation(value = "打印波次汇总拣货单", notes = "打印波次汇总拣货单")
    public void printWavePickingListByTotal(@PathVariable("id") Long id, HttpServletResponse response) throws Exception {
        outboundWaveService.printWavePickingListByTotal(id, response);
    }
}

